$(function() {
	//城市级联
	$.getScript("js/city.js", function(data) {
		var result = JSON.parse(data);
		var cityArr = [];
		var countyArr = [];
		for (var i = 0; i < result.length; i++) {
			if (result[i].ProSort) {
				$("#province").append("<option value='" + result[i].ProID + "'>" + result[i].name + "</option>");
			} else if (result[i].CitySort) {
				cityArr.push(result[i]);
			} else if (result[i].DisSort == null) {
				countyArr.push(result[i]);
			}
		}
		
		$("#province").change(function() {
			var value = $(this).val();
			$("#city option").eq(0).siblings().remove();
			$("#county option").eq(0).siblings().remove();
			for (var i = 0; i < cityArr.length; i++) {
				if (cityArr[i].ProID == value) {
					$("#city").append("<option value='" + cityArr[i].CityID + "'>" + cityArr[i].name + "</option>");
				}
			}
		})

		$("#city").change(function() {
			var value = $(this).val();
			$("#county option").eq(0).siblings().remove();
			for (var i = 0; i < countyArr.length; i++) {
				if (value == countyArr[i].CityID) {
					$("#county").append("<option>" + countyArr[i].DisName + "</option>");
				}
			}
		})
	})

})

var phonetip = document.getElementById("phonetip");
var emailtip = document.getElementById("emailtip");
var nametip = document.getElementById("nametip");
var detailtip = document.getElementById("detailtip");
var protip = document.getElementById("protip");
var citytip = document.getElementById("citytip");
var countytip = document.getElementById("countytip");

function del(obj) {
	var par = obj.parentNode.parentNode.parentNode;
	par.remove();
}

function edit(obj) {
	var addresee = obj.parentNode.parentNode.parentNode.childNodes[0];
	addresee.setAttribute("contenteditable", "true");
	addresee.focus();
	var p = obj.parentNode.parentNode.parentNode.childNodes[1];
	p.setAttribute("contenteditable", "true");
	var tel = obj.parentNode.parentNode.parentNode.childNodes[2];
	tel.setAttribute("contenteditable", "true");
	var mail = obj.parentNode.parentNode.parentNode.childNodes[3];
	mail.setAttribute("contenteditable", "true");
	var code = obj.parentNode.parentNode.parentNode.childNodes[4];
	code.setAttribute("contenteditable", "true");
}

var name_1 = document.getElementById("name");
var phone = document.getElementById("phone");
var detail = document.getElementById("detail_address");
var email = document.getElementById("email");
var reg_email = /^[a-zA-Z\d_]+(\.[a-zA-Z\d_]+)*@([\da-zA-Z](-[\da-zA-Z])?)+(\.{1,2}[a-zA-Z]+)+$/;
var reg_phone = /^1[34578]\d{9}$/;


name_1.onblur = function(){
	if ((name_1.value).trim() == "" ) {
			nametip.style.display = "inline-block";
			return false;
		}else{
			nametip.style.display = "none";
	}
}

phone.onblur = function(){
	if ((phone.value).trim() == "" || !reg_phone.test(phone.value)) {
			phonetip.style.display = "inline-block";
			return false;
		}else{
			phonetip.style.display = "none";
	}
}

detail.onblur = function(){
	if ((detail.value).trim() == "") {
			detailtip.style.display = "inline-block";
			return false;
		}else{
			detailtip.style.display = "none";
		}	
}

email.onblur = function(){
	if (email.value == "" || !reg_email.test(email.value)) {
			emailtip.style.display = "inline-block";
			return false;
		}else{
			emailtip.style.display = "none";
	}
}

function subInput(event) {
	var pro = document.getElementById("province");
	var index = pro.selectedIndex;
	//获取选中的省值
	var pro_text = pro.options[index].text;
	var city = document.getElementById("city");
	var index = city.selectedIndex;
	//获取选中的城市值
	var city_text = city.options[index].text;
	var county = document.getElementById("county");
	var index = county.selectedIndex;
	//获取选中的县值
	var county_text = county.options[index].text;
		
		if ((name_1.value).trim() == "" ) {
			nametip.style.display = "inline-block";
			return false;
		}else{
			nametip.style.display = "none";
		}
		if ((phone.value).trim() == "" || !reg_phone.test(phone.value)) {
			phonetip.style.display = "inline-block";
			return false;
		}else{
			phonetip.style.display = "none";
		}
		if ((detail.value).trim() == "") {
			detailtip.style.display = "inline-block";
			return false;
		}else{
			detailtip.style.display = "none";
		}
		if (email.value == "" || !reg_email.test(email.value)) {
			emailtip.style.display = "inline-block";
			return false;
		}else{
			emailtip.style.display = "none";
		}
		if (pro_text == "--省--") {
			protip.style.display = "inline-block";
			return false;
		}else{
			protip.style.display = "none";
		}
		if (city_text == "--市--") {
			citytip.style.display = "inline-block";
			return false;
		}else{
			citytip.style.display = "none";
		}
		if (county_text == "--区--") {
			countytip.style.display = "inline-block";
			return false;
		}else{
			countytip.style.display = "none";
		}
	
		var li = document.createElement("li");
		var span1 = document.createElement("span");
		var text1 = document.createTextNode(name_1.value);
		span1.setAttribute("class","addressee");
		var p1 = document.createElement("p");
		var pro = document.createTextNode(pro_text);
		var city = document.createTextNode(city_text);
		var county = document.createTextNode(county_text);
		var detail_address = document.createTextNode(detail.value);
		var span2 = document.createElement("span");
		var text2 = document.createTextNode(phone.value);
		span2.setAttribute("class","tel");
		var span3 = document.createElement("span");
		var text3 = document.createTextNode((email.value).toLowerCase())
		span3.setAttribute("class","mail");
		var span4 = document.createElement("span");
		span4.setAttribute("class","code");
		var text4 = document.createTextNode("000000");
		var span5 = document.createElement("span");
		span5.setAttribute("class","operate");
		var span6 = document.createElement("span");
		var a6 = document.createElement("a");
		var text6 = document.createTextNode("编辑");
		a6.setAttribute("href","javascript:void(0)");
		a6.setAttribute("onclick","edit(this)");
		var span7 = document.createElement("span");
		var a7 = document.createElement("a");
		var text7 = document.createTextNode("删除");
		a7.setAttribute("href","javascript:void(0)");
		a7.setAttribute("onclick","del(this)");
		
		span1.appendChild(text1);
		p1.appendChild(pro);
		p1.appendChild(city);
		p1.appendChild(county);
		p1.appendChild(detail_address);
		span2.appendChild(text2);
		span3.appendChild(text3);
		span4.appendChild(text4);
		a6.appendChild(text6);
		span6.appendChild(a6);
		a7.appendChild(text7);
		span7.appendChild(a7);
		span5.appendChild(span6);
		span5.appendChild(span7);
		li.appendChild(span1);
		li.appendChild(p1);
		li.appendChild(span2);
		li.appendChild(span3);
		li.appendChild(span4);
		li.appendChild(span5);
		var exist_address = document.getElementsByClassName("exist_address")[0];
		exist_address.appendChild(li);
}

